package cn.kxx.serviceImpl;

import cn.kxx.config.DateUtil;
import cn.kxx.exception.BusinessException;
import cn.kxx.mapper.OrderSettingMapper;
import cn.kxx.pojo.OrderSetting;
import cn.kxx.service.OrderSettingService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.ParseException;
import java.util.*;

@Service
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingMapper orderSettingMapper;


    //    添加预约信息
    @Override
    public void add(List<String[]> list) throws BusinessException {


        if (list != null && list.size() > 0) {
            for (String[] strs : list) {
                //获取预约日期
                String dateStr = strs[0];
                Date date = null;
                try {
                    date = DateUtil.strToDate(dateStr, "yyyy/MM/dd");
                } catch (ParseException e) {
                    throw new BusinessException("日期格式错误");
                }
                //获取可预约人数
                String numStr = strs[1];
                Integer number = Integer.valueOf(numStr);

                //根据预约日期进行查询，如果有设置信息则修改，否则添加
                OrderSetting one = orderSettingMapper.selectOne(
                        new LambdaQueryWrapper<OrderSetting>().eq(OrderSetting::getOrderDate, date)
                );
                if (one != null) {//如果当前日期有信息，则修改
                    one.setNumber(number);
                    orderSettingMapper.updateById(one);
                } else {
                    OrderSetting orderSetting = new OrderSetting(date, number);
                    orderSettingMapper.insert(orderSetting);
                }
            }
        }
    }

//    展示预约信息
    @Override
    public List<Map> getOrderSettingByMonth(String date) throws BusinessException {
        String dateBegin = date + "-1";//2019-3-1
        String dateEnd = date + "-31";//2019-3-31
        LambdaQueryWrapper<OrderSetting> wrapper = new LambdaQueryWrapper<>();
        wrapper.between(OrderSetting::getOrderDate,dateBegin,dateEnd);
        List<OrderSetting> list = orderSettingMapper.selectList(wrapper);
        List<Map> data = new ArrayList<>();
        for (OrderSetting orderSetting : list) {
            Map orderSettingMap = new HashMap();
            orderSettingMap.put("date",orderSetting.getOrderDate().getDate());//获得日期（几号）
            orderSettingMap.put("number",orderSetting.getNumber());//可预约人数
            orderSettingMap.put("reservations",orderSetting.getReservations());//已预约人数
            data.add(orderSettingMap);
        }
        return data;
    }


//    前端界面设置预约人数
    @Override
    public void editNumberByDate(OrderSetting orderSetting) throws BusinessException {

        Date orderDate = orderSetting.getOrderDate();
        //1.根据日期查询是否有设置信息
        OrderSetting one = orderSettingMapper.getOrderSettingByOrderDate(orderDate);
        if (one!=null){//2.有修改
            one.setNumber(orderSetting.getNumber());
            orderSettingMapper.updateById(one);
        }else{//3.没有则添加
            orderSettingMapper.insert(orderSetting);
        };
    }

}





